<?php
/**
 *userMgmt Model
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * UserMgmt_Model
 * Manages user information
 *@package model
 */
    class UserMgmt_Model extends Model{
                /**
     * Reference to database connection
     * @var object
     */
        private $oDBInstance;
         /**
     * Model constructor
     * Establishes connection to database
     */
        public function __construct(){
            /**
         * Gets the database reference from Mysql_dbconnection
         * @var object
         */
            $this->oDBInstance = Mysql_dbconnection::getInstance();;
        }
/**
 * Gets the page title 
 */
        public function getPageTitle(){              
            return "CMS-User Einstellungen";
        }
        /**
         * Gets user information id, username and password
         * @param string username
         */
        public function getUserInfos($sUsername){
            $sUsername = $this->oDBInstance->getSQLValueString($sUsername, 'string');
            
        if($this->oDBInstance->performSelect('id, username, email','bs_cms_user',"username = $sUsername")){
               $aResult = $this->oDBInstance->fetch('array');
               return $aResult[0];
               
            }
        }
            /**
             * Checks if the password is correct 
             * @param string username
             * @param string password 
             */
        public function checkPwd($sUsername, $sPassword){
           $sUsername = $this->oDBInstance->getSQLValueString($sUsername, 'string');
           
           if($this->oDBInstance->performSelect('password','bs_cms_user',"username = $sUsername")){
               $aResult = $this->oDBInstance->fetch('array');
               $sDBPassword = $aResult[0]['password'];
               if($sDBPassword == $sPassword){
                   return TRUE;
               }else{
                   return FALSE;
               }
           }else{
               return FALSE;
           }
        }
        /**
         * Changes password of the user
         * @param string $sUsername
         * @param string $sNewPassword
         */
        public function changePwd($sUsername, $sNewPassword){
            $sUsername = $this->oDBInstance->getSQLValueString($sUsername, 'string');
            $sNewPassword = $this->oDBInstance->getSQLValueString($sNewPassword, 'string');
            
            $bSuccessful = $this->oDBInstance->query("UPDATE bs_cms_user SET password = $sNewPassword WHERE username = $sUsername"); 
            return $bSuccessful;
        }
        /**
         * Changes username
         * @param string old username
         * @param string new username
         */
        public function changeUsername($sOldUsername, $sNewUsername){
            $iUserID = $this->getUserID($sOldUsername);
            $sNewUsername = $this->oDBInstance->getSQLValueString($sNewUsername, 'string');
            
            $bSuccessful = $this->oDBInstance->query("UPDATE bs_cms_user SET username = $sNewUsername WHERE id = $iUserID");
            return $bSuccessful;
        }
        /**
         * Gets user ID 
         * @param string $sUsername
         */
        public function getUserID($sUsername){
            $sUsername = $this->oDBInstance->getSQLValueString($sUsername, 'string');
            if($this->oDBInstance->performSelect('id','bs_cms_user',"username = $sUsername")){
               $aResult = $this->oDBInstance->fetch('array');
               $sUserID = $aResult[0]['id'];
               
               return $sUserID;
            }
        }
        /**
         * Changes email adress of the username which is also used for pricerequests 
         * @param string username
         * @param string email
         */
    public function changeEmail($sUsername, $sNewEmail){
            $sUsername = $this->oDBInstance->getSQLValueString($sUsername, 'string');
            $sNewEmail = $this->oDBInstance->getSQLValueString($sNewEmail, 'string');
            
            $bSuccessful = $this->oDBInstance->query("UPDATE bs_cms_user SET email = $sNewEmail WHERE username = $sUsername");
            return $bSuccessful;
        }
        
        
    }
?>